<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: 武露斌
  Date: 2021/10/13
  Time: 14:51
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script src="js/jquery-1.8.2.min.js"></script>
    <script src="../../js/fileupload.js"></script>
    <script src="../../css/img.css"></script>
    <link rel="stylesheet" href="css/css.css">
</head>
<body>

<form action="list.do" method="post">
    <input type="hidden" name="pageNum">
    姓名：<input type="text" name="sname" value="${map.sname}">
    生日：<input type="text" name="birs" value="${map.birs}">---<input type="text" name="birl" value="${map.birl}">
    性别：
    <input type="radio" name="sex" value="男" ${map.sex=='男'?'checked':''}>男
    <input type="radio" name="sex" value="女" ${map.sex=='女'?'checked':''}>女
    <br>

    地址：
     <select name="sheng" onchange="sheng2()">
        <option value="">请选择省</option>
    </select>
    <select name="shi" onchange="shi2()">
        <option value="">请选择市</option>
    </select>
    <select name="xian">
        <option value="">请选择县</option>
    </select>

    <input type="submit" value="查询">
    <input type="button" value="刷新" onclick="sx()">
</form>


<table border="1">
    <tr>
        <td>编号</td>
        <td>姓名</td>
        <td>照片</td>
        <td>性别</td>
        <td>爱好</td>
        <td>生日</td>
        <td>课程</td>
        <td>地址</td>
        <td>
            <input type="button" value="添加" onclick="add()">
        </td>
    </tr>
    <c:forEach items="${pageInfo.list}" var="a">
        <tr>
            <td>${a.sid}</td>
            <td>${a.sname}</td>
            <td>
                <img src="${a.pic}" width="60px">
            </td>
            <td>${a.sex}</td>
            <td>${a.hobby}</td>
            <td>${a.bir}</td>
            <td>${a.kcs}</td>
            <td>${a.shengName}-${a.shiName}-${a.xianName}</td>
            <td>
                <input type="button" value="删除" onclick="del(${a.sid})">
                <input type="button" value="修改" onclick="upda(${a.sid})">
            </td>
        </tr>
    </c:forEach>

    <tr>
        <td colspan="20">
            第${pageInfo.pageNum}/${pageInfo.pages}页
            <input type="button" value="首页" onclick="fy(1)">
            <input type="button" value="上一页" onclick="fy(${pageInfo.isFirstPage?1:pageInfo.prePage})">
            <input type="button" value="下一页" onclick="fy(${pageInfo.isLastPage?pageInfo.pages:pageInfo.nextPage})">
            <input type="button" value="尾页" onclick="fy(${pageInfo.pages})">
        </td>
    </tr>
</table>

</body>
</html>

<script>

    $.post(
        "listcity.do",
        {"pid":0},
        function (data) {
            for (var i = 0; i <data.length ; i++) {
                $("[name='sheng']").append(' <option value="'+data[i].id+'">'+data[i].name+'</option>')
            }
            $("[name='sheng']").val(${map.sheng})
            $("[name='sheng']").trigger("change")
        }
    )

    function sheng2() {
        pid=$("[name='sheng']").val()
        $("[name='shi']").empty()
        $("[name='shi']").append('<option value="">请选择市</option>')
        $("[name='xian']").empty()
        $("[name='xian']").append('<option value="">请选择县</option>')
        $.post(
            "listcity.do",
            {"pid":pid},
            function (data) {

                for (var i = 0; i <data.length ; i++) {
                    $("[name='shi']").append(' <option value="'+data[i].id+'">'+data[i].name+'</option>')
                }
                $("[name='shi']").val(${map.shi})
                $("[name='shi']").trigger("change")
            }
        )
    }
    function shi2() {
        pid=$("[name='shi']").val()
        $("[name='xian']").empty()
        $("[name='xian']").append('<option value="">请选择县</option>')
        $.post(
            "listcity.do",
            {"pid":pid},
            function (data) {
                for (var i = 0; i <data.length ; i++) {
                    $("[name='xian']").append(' <option value="'+data[i].id+'">'+data[i].name+'</option>')
                }
                $("[name='xian']").val(${map.xian})
            }
        )
    }

    function del(sid) {
        if (confirm("确认要删除ID为"+sid+"学生吗？")){
            $.ajax({
                url:"delstu.do",
                data:{"sid":sid},
                type:"post",
                dataType:"json",
                success:function (data) {
                    if (data>0){
                        alert("删除成功")
                        location="list.do"
                    }else{
                        alert("删除失败")
                    }
                }

            })
        }else{
            alert("已经取消")
        }
    }

    function add() {
        location="addto.do"
    }

    function sx() {
        location="list.do"
    }
    function fy(pageNum) {
        $("[name='pageNum']").val(pageNum)
        $("form").submit();
    }

    function upda(sid) {
        location="updato.do?sid="+sid;
    }
</script>
